package com.example.template.ui.home.page;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.library.baseAdapters.BR;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.base.adapter.FragmentPagerAdapter;
import com.example.base.adapter.indicator.WrapIndicator;
import com.example.base.http.ApiObserver;
import com.example.base.http.HttpClient;
import com.example.base.http.ResponseListData;
import com.example.base.ui.BaseFragment;
import com.example.base.ui.viewbinding.DataBindingConfig;
import com.example.base.utils.ToastUtils;
import com.example.common.constant.Constant;
import com.example.template.R;
import com.example.template.databinding.FragmentHomeBinding;
import com.example.template.http.entity.CommunityEntity;
import com.example.template.http.entity.CommunityListData;
import com.example.template.http.entity.TradeEntity;
import com.example.template.http.service.CommunityApiService;
import com.example.template.http.service.HomeApiService;
import com.example.template.ui.home.adapter.HomeTradeAdapter;
import com.example.template.ui.home.state.HomeVewModel;
import com.example.template.ui.market.adapter.MarketAdapter;
import com.example.template.ui.market.page.MarketListFragment;

import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import java.util.Arrays;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class HomeFragment extends BaseFragment {

    private HomeVewModel viewModel;
    private FragmentHomeBinding binding;
    private HomeTradeAdapter adapter;

    @Override
    protected void initViewModel() {
        viewModel = getFragmentScopeViewModel(HomeVewModel.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.fragment_home, BR.vm,viewModel);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding = (FragmentHomeBinding) getBinding();

        binding.toolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToastUtils.toast("点击返回");
            }
        });

        // 跟单
        adapter = new HomeTradeAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false);
        binding.recyclerTrade.setLayoutManager(linearLayoutManager);
        binding.recyclerTrade.setAdapter(adapter);
        adapter.setEmptyView(R.layout.common_empty_view);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                ToastUtils.toast("点击：" +position);
            }
        });

        // 行情
        String[] mTabs = getResources().getStringArray(R.array.home_tabs);
        FragmentPagerAdapter<BaseFragment> mPagerAdapter = new FragmentPagerAdapter<>(this);
        mPagerAdapter.addFragment(MarketListFragment.newInstance());
        mPagerAdapter.addFragment(MarketListFragment.newInstance());
        binding.viewPager.setAdapter(mPagerAdapter);
        CommonNavigator commonNavigator = new CommonNavigator(getContext());
        commonNavigator.setAdapter(new WrapIndicator(binding.viewPager, Arrays.asList(mTabs)));
        binding.magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(binding.magicIndicator, binding.viewPager);

        getData();
    }

    private void getData() {
        HttpClient.getInstance().create(HomeApiService.class)
                .getTeamList(0,10,1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ApiObserver<ResponseListData<TradeEntity>>() {
                    @Override
                    public void onResult(ResponseListData<TradeEntity> listResponseData) {
                        LogUtils.i(listResponseData.toString());
                        adapter.setDiffNewData(listResponseData.getResult().getItems());
                    }
                });
    }
}